import React from 'react';
import { Provider } from 'react-redux';
import { renderToString } from 'react-dom/server';
import { StaticRouter } from 'react-router-dom';
import { renderRoutes } from 'react-router-config';
import serialize from 'serialize-javascript';
import routes from '../share/routes';

export default function(req, store) {
  const content = renderToString(
    <Provider store={store}>
      <StaticRouter location={req.path}>
        { renderRoutes(routes) }
      </StaticRouter>
    </Provider>
  );
  // 防止XXS攻击
  const state = serialize(store.getState());
  return (`
    <html>
      <head>
        <title>React SSR</title>
      </head>
      <body>
        <div id="root">${content}</div>
        <script>window.INIT_STATE = ${state}</script>
        <script src="bundle.js"></script>
      </body>
    </html>
  `);
}